package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.PreRemoveMachineInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.AssemblyAttrDtoUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.I18nUtil;
import com.ztesoft.bss.svcability.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * 预销户原因模块
 *
 */
@Service
public class PreRemoveMachineInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public List<PreRemoveMachineInfoVO> execute(AcceptContext context, SceneModule sceneModule) {
        PreRemoveMachineInfoVO preRemoveMachineInfoVO = new PreRemoveMachineInfoVO();
        List<PreRemoveMachineInfoVO> resultList = new ArrayList<>();

        List<KiteAttr> commAttrs = querySceneInstService.listCommonAttrs(context.getSceneVo().getSceneInstId(), LocalKeyConsts.REMOVEMACHINE_OBJECT_TYPE);
        commAttrs = Optional.ofNullable(commAttrs).orElse(new ArrayList<>());

        // 去掉下拉框值 "请选择" 的空选项
        if (!ListUtil.isEmpty(commAttrs)) {
            for (KiteAttr kiteAttr: commAttrs) {
                if (!KeyConsts.DROPDOWN.equals(kiteAttr.getAttrValueTypeId())) {
                    continue;
                }
                List<KiteAttrValue> attrValues = kiteAttr.getAttrValues();
                if (KiteListUtils.isEmpty(attrValues)) {
                    continue;
                }
                Iterator<KiteAttrValue> iter = attrValues.iterator();
                while (iter.hasNext()) {
                    KiteAttrValue attrValue = iter.next();
                    if (I18nUtil.getMessage("BSS_KITE_ATTR_43033").equals(attrValue.getAttrValueName())) {
                        iter.remove();
                    }
                }
            }
        }

        List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commAttrs);       
        preRemoveMachineInfoVO.setPreRemoveMachineAttr(instAttrDTOList);

        SceneNewOfferSeneVO vo = (SceneNewOfferSeneVO) context.getSceneVo();
        vo.setPreRemoveMachineInfo(preRemoveMachineInfoVO);
        resultList.add(preRemoveMachineInfoVO);
        return resultList;
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        PreRemoveMachineInfoVO preRemoveMachineInfoVO = new PreRemoveMachineInfoVO();

        List<KiteAttr> commAttrs = querySceneInstService.listCommonAttrs(context.getSceneVo().getSceneInstId(), LocalKeyConsts.REMOVEMACHINE_OBJECT_TYPE);
        commAttrs = Optional.ofNullable(commAttrs).orElse(new ArrayList<>());

        List<InstAttrDTO> instAttrDTOList = AssemblyAttrDtoUtils.assemblyInstAttr(commAttrs);
        preRemoveMachineInfoVO.setPreRemoveMachineAttr(instAttrDTOList);

        SceneNewOfferSeneVO vo = (SceneNewOfferSeneVO) context.getSceneVo();
        vo.setPreRemoveMachineInfo(preRemoveMachineInfoVO);

        return Collections.singletonList(preRemoveMachineInfoVO);
    }

}
